import Vue from 'vue';
import Vuex from 'vuex';
import * as actions from './actions';
import * as getters from './getters';

Vue.use(Vuex);

// 应用初始状态
const state = {
  token: '',      // 保存服务器给的token
  returnPath: localStorage.returnPath || ''  // 重新登录之后返回的url
};

// 定义所需的 mutations
const mutations = {
  SETTOKEN (state, token) {
    state.token = token
    // 保存token
    localStorage.setItem('token', token)
  },
  SETRETURNPATH (state, url) {
    state.returnPath = localStorage.returnPath = url
  }
};

// 创建 store 实例
export default new Vuex.Store({
  actions,
  getters,
  state,
  mutations
});
